//
//  FileOLPreViewController.swift
//  AutoPrint
//
//  Created by David on 16/10/7.
//  Copyright © 2016年 fatcat. All rights reserved.
//  在线文件预览

import UIKit

class FileOLPreViewController: UIViewController {
    
    var filePath: String!
    var fileName: String!
    var fileMD5: String!
    
    var fileIcon:UIImageView!
    var fileNameLabel:UILabel!
    var fileInfoLabel:UILabel!
    
    var previewBtn:UIButton!
    var postBtn:UIButton!
    var shareBtn:UIButton!
    
    convenience init(name: String, path: String, md5: String){
        self.init()
        filePath = path
        fileName = name
        fileMD5 = md5
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        title = fileName
        
        
        fileIcon = UIImageView(frame: CGRect(x: 0, y: 64+16, width: 72, height: 72))
        fileIcon.center.x = view.center.x
        fileIcon.backgroundColor = UIColor.blueColor()
        view.addSubview(fileIcon)
        
        let fileType = fileName.componentsSeparatedByString(".").last
        if fileType != nil {
            if fileType == "doc" || fileType == "docx" {
                fileIcon.backgroundColor = UIColor.clearColor()
                fileIcon.image = UIImage(named: "documents_icon_doc")
            }else if fileType == "xls" || fileType == "xlsx" {
                fileIcon.backgroundColor = UIColor.clearColor()
                fileIcon.image = UIImage(named: "documents_icon_xls")
            }else if fileType == "ppt" || fileType == "pptx" {
                fileIcon.backgroundColor = UIColor.clearColor()
                fileIcon.image = UIImage(named: "documents_icon_ppt")
            }else if fileType == "pdf"{
                fileIcon.backgroundColor = UIColor.clearColor()
                fileIcon.image = UIImage(named: "documents_icon_pdf")
            }else if fileType == "txt"{
                fileIcon.backgroundColor = UIColor.clearColor()
                fileIcon.image = UIImage(named: "documents_icon_txt")
            }
        }
        
        fileNameLabel = UILabel(frame: CGRect(x: 0, y: 64+16+72+16, width: view.bounds.width - 32, height: 20))
        fileNameLabel.center.x = view.center.x
        //        fileNameLabel.backgroundColor = UIColor.greenColor()
        fileNameLabel.text = fileName
        fileNameLabel.textAlignment = .Center
        fileNameLabel.lineBreakMode = .ByTruncatingMiddle
        view.addSubview(fileNameLabel)
        
        fileInfoLabel = UILabel(frame: CGRect(x: 0, y: 64+16+72+16+20+16, width: view.bounds.width - 32, height: 20))
        fileInfoLabel.center.x = view.center.x
        fileInfoLabel.textColor = UIColor.lightGrayColor()
        //        fileInfoLabel.backgroundColor = UIColor.brownColor()
        fileInfoLabel.textAlignment = .Center
        view.addSubview(fileInfoLabel)
        
        

        fileInfoLabel.text = "在线文件"
        
        previewBtn = UIButton(frame: CGRect(x: 0, y: view.bounds.height-64-48, width: view.bounds.width - 32, height: 48))
        previewBtn.center.x = view.center.x
        previewBtn.backgroundColor = UIColor.lightGrayColor()
        previewBtn.layer.cornerRadius = 4
        previewBtn.setTitle("在线预览", forState: .Normal)
        view.addSubview(previewBtn)
        previewBtn.addTarget(self, action: #selector(FilePreViewController.openPreView), forControlEvents: .TouchUpInside)
        
        postBtn = UIButton(frame: CGRect(x: 0, y: view.bounds.height-64-48-16-48, width: view.bounds.width-32, height: 48))
        postBtn.center.x = view.center.x
        postBtn.backgroundColor = UIColor.orangeColor()
        postBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        postBtn.layer.cornerRadius = 4
        postBtn.setTitle("提交打印", forState: .Normal)
        view.addSubview(postBtn)
        postBtn.addTarget(self, action: #selector(FilePreViewController.postFile), forControlEvents: .TouchUpInside)
    }
    
    
    
    func openPreView(){
        presentViewController(HTMLStringViewController(path: filePath, name: fileName), animated: true, completion: nil)
    }
    
    
    func postFile(){
        if NSUserDefaults.standardUserDefaults().objectForKey("user") == nil {
            presentViewController(UDLoginViewController(), animated: true, completion: nil)
        }else{
            hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(FileReadyViewController(files: [UDFile(filePath: filePath, fileName: fileName, fileSize: "在线文件", fileMD5: fileMD5)], step0VC: self), animated: true)
        }
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

